package com.example.community.list;

import com.example.community.dao.ComplaintDao;
import com.example.community.dao.UsersDao;
import com.example.community.model.Complaint;
import com.example.community.model.Users;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet(urlPatterns = "/complaintListServlet")
public class ComplaintListServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Integer ys = null;
        if (req.getParameter("ys")==null){
            ys = 1 ; //当前页数
        }else {
            ys = Integer.parseInt(req.getParameter("ys"));
        }
        int shuju = 8; //每页显示数据个数
        ComplaintDao complaintDao = new ComplaintDao();
        List<Complaint> complaints = complaintDao.fandLimit(ys, shuju);
        for (Complaint complaint:complaints){
            UsersDao usersDao = new UsersDao();
            Users users = usersDao.fandById(complaint.getUsNo());
            complaint.setUsName(users.getUsName());
        }
        Integer totalPageNum = complaintDao.selectLimitCount(shuju);
        req.setAttribute("complaints",complaints);
        req.setAttribute("totalPageNum",totalPageNum);
        req.setAttribute("ys",ys);
        req.getRequestDispatcher("/WEB-INF/communityService/complaintList.jsp").forward(req,resp);
    }
}
